package com.heatup.apt.api.service.impl;

import com.heatup.apt.common.dao.ProductServiceMapper;
import com.heatup.apt.common.dao.user.UserProductServiceMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.model.product.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.model.user.UserProductService;
import com.heatup.apt.api.service.UserProductServiceService;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * UserProductService 表数据服务层接口实现类
 */
@Service
public class UserProductServiceServiceImpl extends SuperServiceImpl<UserProductServiceMapper, UserProductService> implements UserProductServiceService {
    @Autowired
    private ProductServiceMapper productServiceMapper;

    @Autowired
    private UserProductServiceMapper userProductServiceMapper;


    private final static  Logger logger = LoggerFactory.getLogger(UserProductServiceServiceImpl.class);


    public Map<String,Object> addUserMonthTeamCourseProductDays(Integer userId, int dayNum, Integer storeId) {

        Map<String,Object> resultMap = new HashMap<String,Object>();
        ProductService productService = null;
        UserProductService userProductService = null;
        UserProductService oldProductService = null;
        Date vipStartDate = null;
        Date vipEndDate = null;
        //查找该门店对应的团课包月服务
        productService = productServiceMapper.queryMonthlyTeamCourseProductServiceByStoreId(storeId);
        if (productService != null && dayNum > 0) {
            userProductService = userProductServiceMapper.queryUserProductServiceByUserIdAndType(userId, productService.getPServiceType());
            oldProductService = userProductService;
            if (userProductService != null) {
                //如果存在该类型的优惠券，则需要在此基础上直接增加时间段。
                //如果记录是有效的，则在endDate加上天数
                if (userProductService.getIsvalid() == 1) {
                    vipStartDate = DateUtils.getDateShort(userProductService.getEndDate());
                    vipEndDate = DateUtils.AddDays(userProductService.getEndDate(), dayNum);
                    userProductService.setEndDate(vipEndDate);
                    userProductService.setUpdatedBy(userId);
                    userProductService.setUpdatedTime(new Date());
                    userProductService.setIsvaild(1);
                } else {
                    vipStartDate = DateUtils.getDateShort(new Date());
                    vipEndDate = DateUtils.AddDays(new Date(), dayNum);
                    userProductService.setBeginDate(vipStartDate);
                    userProductService.setEndDate(vipEndDate);
                    userProductService.setUpdatedBy(userId);
                    userProductService.setUpdatedTime(new Date());
                    userProductService.setIsvaild(1);
                }
                userProductServiceMapper.updateById(userProductService);
            } else {
                //从未购买过该产品，则需要增加该产品的记录
                userProductService = new UserProductService();
                userProductService.setProductServiceType(productService.getPServiceType());
                userProductService.setUserId(userId);
                vipStartDate = DateUtils.getDateShort(new Date());
                vipEndDate = DateUtils.AddDays(vipStartDate, dayNum);
                userProductService.setIsvaild(1);
                userProductService.setBeginDate(vipStartDate);
                userProductService.setEndDate(vipEndDate);
                userProductService.setCreatedBy(userId);
                userProductService.setCreatedTime(new Date());
                //插入到userProductService
                userProductServiceMapper.insert(userProductService);
            }
        }

        resultMap.put("useBeginDate",vipStartDate);
        resultMap.put("useEndDate",vipEndDate);
        resultMap.put("productService",productService);
        resultMap.put("userProductService",userProductService);

        return resultMap;
    }

    
	@Override
	public UserProductService queryUserProductServiceByUserIdAndType(Integer userId, String type) {
		// 
		return userProductServiceMapper.queryUserProductServiceByUserIdAndType(userId, type);
	}

}